package net.mihome.app.bean;

import net.mihome.app.utils.Tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

public class RssNews {
	private long id;
	private String title;
	private String href;
	private String description;
	private String pubDates;
    private String thumbPic;
    private String text;


	private static SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'",Locale.US);  
	private static SimpleDateFormat sdf2=new SimpleDateFormat("yyyy年MM月dd HH日mm分");
	public String getPubDates() {
		return pubDates;
	}
	public void setPubDates(String pubDates) {
		this.pubDates = pubDates;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getHref() {
		return href;
	}
	public void setHref(String href) {
		this.href = href;
	}
	public static List<RssNews> parse(String xmlstr){
		List<RssNews> list=new ArrayList<RssNews>();
		List<String> itemTitlesList=Tools.listByRes(xmlstr,"<item>.+?</item>");
		Iterator<String> iterator=itemTitlesList.iterator();
		while(iterator.hasNext()){
			RssNews rssNews=new RssNews();
			String newsStr=iterator.next();
			String title=Tools.getByRex(newsStr,"<title>.+?</title>");
			String href=Tools.getByRex(newsStr,"<link>.+?</link>");
			String description=Tools.getByRex(newsStr,"<description>.+?</description>");
			String pubDate=Tools.getByRex(newsStr,"<pubDate>.+?</pubDate>");
			if(title!=null){
				title=title.replaceAll("(</title>)|(<title>)|(<!\\[CDATA\\[)|(\\]\\]>)","");
				title=title.trim();
			}
			if(href!=null){
				href=href.replaceAll("(</link>)|(<link>)|(<!\\[CDATA\\[)|(\\]\\]>)","");
				href=href.trim();
			}
			if(description!=null){
				description=description.replaceAll("(</description>)|(<description>)|(<!\\[CDATA\\[)|(\\]\\]>)","");
				description=description.trim();
			}
			if(pubDate!=null){
				pubDate=pubDate.replaceAll("(</pubDate>)|(<pubDate>)|(<!\\[CDATA\\[)|(\\]\\]>)","");
				pubDate=pubDate.trim();
			}
			rssNews.setTitle(title);
			rssNews.setHref(href);
			rssNews.setDescription(description);
			try {
				sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
                if(pubDate!=null){
                    Date date=sdf.parse(pubDate);
                    rssNews.setPubDates(sdf2.format(date));
                }else{
                    rssNews.setPubDates(pubDate);
                }
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				rssNews.setPubDates(pubDate);
			}
			list.add(rssNews);
		}
		return list;
	}
    public static RssNews parse(Map map){
        RssNews rssNews=new RssNews();
        Double idsDouble=(Double) map.get("id");
        rssNews.setId(idsDouble.intValue());
        rssNews.setPubDates((String) map.get("publish_date"));
        rssNews.setDescription((String) map.get("description"));
        rssNews.setHref((String) map.get("href"));
        rssNews.setTitle((String) map.get("title"));
        rssNews.setThumbPic((String) map.get("thumb_pic"));
        rssNews.setText((String) map.get("text"));
        return rssNews;
    }

    public static List<RssNews> parseList(List<Map> maps){
        List<RssNews> list=new ArrayList<>();
        if(maps!=null){
            Iterator<Map> iterator=maps.iterator();
            while(iterator.hasNext()){
                Map map=iterator.next();
                RssNews rssNews=parse(map);
                list.add(rssNews);
            }
        }
        return list;
    }
    public String getThumbPic() {
        return thumbPic;
    }

    public void setThumbPic(String thumbPic) {
        this.thumbPic = thumbPic;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}
